package com.alumni_circle.controller.gossip;

import cn.hutool.core.util.StrUtil;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.Modify;
import com.alumni_circle.core.utils.PictureBase64;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.gossip.Gossip;
import com.alumni_circle.entity.gossip.VO.GossipInsertVO;
import com.alumni_circle.entity.gossip.VO.GossipUpdateVO;
import com.alumni_circle.entity.gossip.VO.GossipViewVO;
import com.alumni_circle.entity.home.issue.UserGossip;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.service.gossip.IGossipService;
import com.alumni_circle.service.home.issue.IGossipCommentsService;
import com.alumni_circle.service.home.issue.IUserGossipService;
import com.alumni_circle.utils.MsgPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.alumni_circle.core.constant.ResponseCode.PERMISSIONS;

/**
 * @author 龙小虬
 * @since 2020-07-07 13:33
 */
@Api(tags = "话题管理")
@RestController
@RequestMapping("/gossip")
public class GossipController extends BaseController<Gossip> {

    @Resource
    MsgPermissions msgPermissions;

    @Resource
    private IGossipService gossipService;

    @Resource
    private IUserGossipService userGossipService;

    @Resource
    private IGossipCommentsService gossipCommentsService;

    /**
     * 增加信息
     * @param gossipInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addGossip", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增杂谈信息", notes = "增加杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="新增杂谈信息",url="/gossip/addGossip")
    public ResponseModel addGossip(@ApiParam(required = true, name = "gossipInsertVO")
                                 @RequestBody GossipInsertVO gossipInsertVO) {
        String path = "";
        if(!StrUtil.hasEmpty(gossipInsertVO.getPicture()) && gossipInsertVO.getPicture().contains("{-temporary}")) {
            path = gossipInsertVO.getPicture();
            gossipInsertVO.setPicture(Modify.modifyImageIndex(path));
        }
        User user = SecurityUtils.getUser();
        Gossip gossip = ModelUtils.transform(gossipInsertVO,Gossip.class);
        gossip.setCreateBy(user.getId());
        gossip.setCreateName(user.getName());
        gossipService.save(gossip);
        super.picturePath(0,gossip.getId(),path);
        UserGossip userGossip = new UserGossip();
        userGossip.setGossipId(gossip.getId());
        userGossip.setUserId(user.getId());
        userGossipService.save(userGossip);
        return new ResponseModel<>("success");
    }

    /**
     * 更新信息
     * @param gossipUpdateVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateGossip", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新杂谈信息", notes = "更新杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新杂谈信息",url="/gossip/updateGossip")
    public ResponseModel updateGossip(@ApiParam(required = true, name = "GossipUpdateVO")
                                    @RequestBody GossipUpdateVO gossipUpdateVO) {

        if(msgPermissions.updateDelete(gossipUpdateVO.getId(), "gossip")) {
            return new ResponseModel<>(PERMISSIONS.getCode(), PERMISSIONS.getMessage());
        }
        boolean isSuccess = gossipService.updateById(ModelUtils.transform(gossipUpdateVO, Gossip.class));
        return new ResponseModel<>("success");
    }

    /**
     * 更新信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateGossipPraise", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新杂谈信息", notes = "更新杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新杂谈信息",url="/gossip/updateGossipPraise")
    public ResponseModel updateGossipPraise(@ApiParam(required = true, name = "GossipUpdateVO")
                                      @RequestBody BaseId baseId) {
        gossipService.updateGossipPraise(baseId.getId());
        return new ResponseModel<>("success");
    }

    /**
     * 更新信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateGossipViews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新杂谈信息", notes = "更新杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新杂谈信息",url="/gossip/updateGossipViews")
    public ResponseModel updateGossipViews(@ApiParam(required = true, name = "baseId")
                                                @RequestBody BaseId baseId) {
        gossipService.updateGossipViews(baseId.getId());
        return new ResponseModel<>("success");
    }

    /**
     * 查询信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectGossip", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询杂谈信息", notes = "查询杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="新增评论信息",url="/gossip/selectGossip")
    public ResponseModel selectGossip(@ApiParam(required = true, name = "pageBase")
                                      @RequestBody BaseId baseId) {

        Gossip gossip = gossipService.getById(baseId.getId());
        GossipViewVO gossipViewVO = ModelUtils.transform(gossip, GossipViewVO.class);
        return new ResponseModel<>(gossipViewVO);
    }


    /**
     * 删除信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteGossip", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除杂谈信息", notes = "删除杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除杂谈信息",url="/gossip/deleteGossip")
    public ResponseModel deleteGossip(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {

        if(msgPermissions.updateDelete(baseId.getId(), "gossip")){
            return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
        }

        gossipService.removeById(baseId.getId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("gossip_id",baseId.getId());
        userGossipService.remove(queryWrapper);
        gossipCommentsService.remove(queryWrapper);
        super.picturePathDelete(baseId.getId());
        return new ResponseModel();
    }

    /**
     * 所有杂谈信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectListGossip", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询杂谈信息", notes = "查询杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询杂谈信息",url="/gossip/selectListGossip")
    public ResponseListModel selectListGossip(@ApiParam(required = true, name = "pageBase")
                                              @RequestBody PageBase pageBase) {

        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<GossipViewVO> list = gossipService.selectListGossip();
        PageInfo<GossipViewVO> pageInfo = new PageInfo<>(list);
        for (GossipViewVO gossipViewVO : list) {
            if (!StrUtil.hasEmpty(gossipViewVO.getPicture())) {
                gossipViewVO.setPicture(PictureBase64.thumbnailList(gossipViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    /**
     * 查询用户本人发布的信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectMyListGossip", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询用户本人发布的杂谈信息", notes = "查询用户本人发布的杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询用户本人发布的杂谈信息",url="/gossip/selectMyListGossip")
    public ResponseListModel selectMyListGossip(@ApiParam(required = true,name = "pageBase")
                                                @RequestBody PageBase pageBase) {

        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<GossipViewVO> list = gossipService.selectMyListGossip(SecurityUtils.getId());
        for (int i = 0; i < list.size(); i++) {
            if(StrUtil.hasEmpty(list.get(i).getPicture())){
                continue;
            }
            list.get(i).setPicture(PictureBase64.thumbnailList(list.get(i).getPicture()));
        }
        return new ResponseListModel<>(list);
    }

    /**
     * 搜索用户本人发布的杂谈信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchMyListGossip", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索用户本人发布的杂谈信息", notes = "搜索用户本人发布的杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索用户本人发布的杂谈信息",url="/gossip/searchMyListGossip")
    public ResponseListModel searchMyListGossip(@ApiParam(required = true,name = "pageBase")
                                                @RequestBody PageBase pageBase) {

        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<GossipViewVO> list = gossipService.searchMyListGossip(SecurityUtils.getId(),pageBase.getContent());
        for (int i = 0; i < list.size(); i++) {
            if(StrUtil.hasEmpty(list.get(i).getPicture())){
                continue;
            }
            list.get(i).setPicture(PictureBase64.thumbnailList(list.get(i).getPicture()));
        }
        return new ResponseListModel<>(list);
    }




    @ResponseBody
    @RequestMapping(value = "/getAllGossip", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询所有杂谈信息", notes = "查询符合条件杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询所有杂谈信息",url="/gossip/getAllGossip")
    public ResponseListModel getAllGossip(@ApiParam(required = true, name = "baseId")
                                          @RequestBody BaseId baseId) {
//        return new ResponseListModel<>(gossipService.list());
        return null;
    }


    @ResponseBody
    @RequestMapping(value = "/selectGossipWrapper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询杂谈信息", notes = "查询杂谈信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询杂谈信息",url="/gossip/selectGossipWrapper")
    public ResponseListModel selectGossipWrapper(@ApiParam(required = true, name = "gossip")
                                                 @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        return new ResponseListModel<>(gossipService.list(new QueryWrapper<Gossip>().and(wrapper -> wrapper.like("title",pageBase.getContent())).or().like("content",pageBase.getContent())));
    }

}

